package com.learning.spring.cloud.alibaba.sentinel.order.web.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.learning.spring.cloud.alibaba.sentinel.order.web.model.Order;
import com.learning.spring.cloud.alibaba.sentinel.order.web.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: UserController
 * Description:
 * Date: 2021/7/25 23:03 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@RestController
@RequestMapping("order")
@RefreshScope
@SentinelResource
public class OrderController {

    @Autowired
    private IOrderService orderService;

    /**
     * @param order
     * @return
     */
    @PostMapping("/save")
    public Integer insert(@RequestBody Order order) {
        return orderService.insert(order);
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public Integer deleteByPrimaryKey(@PathVariable("id") Long id) {
        return orderService.deleteByPrimaryKey(id);
    }

    /**
     * Sentinel 功能支持
     * http://localhost:8002/order/query/1
     *
     * @param id
     * @return
     */
    @GetMapping("/query/{id}")
    public Order selectByPrimaryKey(@PathVariable("id") Long id) {
        return orderService.selectByPrimaryKey(id);
    }

    @GetMapping("/query/user/{userId}")
    public List<String> queryByUserId(@PathVariable("userId") Long userId) {
        return orderService.queryByUserId(userId).stream().map(Order::getName).collect(Collectors.toList());
    }

    @Value("${project.demo.userName:sam}")
    private String userName;

    /**
     * 查询在 配置中心 刷新的配置
     * http://localhost:8002/order/query/config
     *
     * @return
     */
    @GetMapping("/query/config")
    public String queryConfig() {
        return userName;
    }
}
